package com.xjrsoft.module.visual.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.Enum.EnabledMarkEnum;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.base.entity.XjrBaseDesktopscheme;
import com.xjrsoft.module.base.entity.XjrBaseDesktopschemeinfo;
import com.xjrsoft.module.base.entity.XjrBaseModule;
import com.xjrsoft.module.base.entity.XjrBaseSubsystem;
import com.xjrsoft.module.base.service.IXjrBaseDesktopschemeService;
import com.xjrsoft.module.base.service.IXjrBaseDesktopschemeinfoService;
import com.xjrsoft.module.base.service.IXjrBaseModuleService;
import com.xjrsoft.module.base.service.IXjrBaseSubsystemService;
import com.xjrsoft.module.visual.entity.XjrVisualHomeRelation;
import com.xjrsoft.module.visual.entity.XjrVisualRelease;
import com.xjrsoft.module.visual.service.IXjrVisualHomeRelationService;
import com.xjrsoft.module.visual.service.IXjrVisualReleaseService;
import com.xjrsoft.module.visual.vo.VisualHomeRelationVo;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/visual-homerelation")
@AllArgsConstructor
public class XjrVisualHomeRelationController {

    private final IXjrVisualHomeRelationService homeRelationService;
    private final IXjrVisualReleaseService visualReleaseService;
    private final IXjrBaseModuleService baseModuleService;
    private final IXjrBaseDesktopschemeService desktopschemeService;
    private final IXjrBaseDesktopschemeinfoService desktopschemeinfoService;
    private final IXjrBaseSubsystemService subsystemService;

    @PutMapping("/{id}/cur")
    @ApiOperation("设置当前用户的首页, 0-主系统，1-子系统")
    public Response updateCurUserSetting(@PathVariable String id) {
        String userId = SecureUtil.getUserId();
        XjrVisualHomeRelation homeRelation = homeRelationService.getHomeRelationByUserId(userId);
        if (homeRelation == null) {
            homeRelation = new XjrVisualHomeRelation();
            homeRelation.setUserId(userId);
        }
        homeRelation.setVisualReleaseId(id);
        return Response.status(homeRelationService.saveOrUpdate(homeRelation));
    }

    @GetMapping("/current")
    @ApiOperation("查询当前用户的首页, 0-主系统，1-子系统")
    public Response<VisualHomeRelationVo> getHomeRelationOfCurUser(@RequestParam(name = "systemId", required = false, defaultValue = "0") String systemId) {
        String userId = SecureUtil.getUserId();
        XjrVisualHomeRelation homeRelation = homeRelationService.getHomeRelationByUserId(userId);
        VisualHomeRelationVo result = null;
        if (homeRelation != null) {
            result = BeanUtil.copy(homeRelation, VisualHomeRelationVo.class);
        } else {
            //获取默认首页
            XjrVisualHomeRelation homeRelationByUserId = homeRelationService.getHomeRelationByUserId("");
            if (homeRelationByUserId != null) {
                result = BeanUtil.copy(homeRelationByUserId, VisualHomeRelationVo.class);
            }
        }
        return Response.ok(result);
    }

    @GetMapping("/index-url")
    @ApiOperation("查询当前用户的首页url, 0-主系统，1-子系统")
    public Response getVisualUrlByCurUser(@RequestParam(name = "systemId", required = false, defaultValue = "0") String systemId) {
        String userId = SecureUtil.getUserId();
        XjrVisualHomeRelation homeRelation = homeRelationService.getHomeRelationByUserId(userId);
        JSONObject result = new JSONObject();
        if (StringUtil.equals("0", systemId)) {
            //主系统
            if (homeRelation == null) {
                // 获取默认首页
                homeRelation = homeRelationService.getHomeRelationByUserId("defaultCurrent");
            }
            if(homeRelation!=null) {
                XjrVisualRelease visualRelease = visualReleaseService.getById(homeRelation.getVisualReleaseId());
                if (visualRelease != null) {
                    if (visualRelease.getCategory() != null && visualRelease.getCategory() == 2) {
                        //桌面设计
                        String kanBanId = visualRelease.getKanBanId();
                        XjrBaseDesktopschemeinfo xjrBaseDesktopschemeinfo = desktopschemeinfoService.getOne(Wrappers.<XjrBaseDesktopschemeinfo>query().lambda().eq(XjrBaseDesktopschemeinfo::getSchemeId, kanBanId).eq(XjrBaseDesktopschemeinfo::getEnabledMark, 1));
                        if (xjrBaseDesktopschemeinfo != null) {
                            result.put("type", 2);
                            result.put("value", xjrBaseDesktopschemeinfo.getContent());
                        }
                    } else {
                        //BI
                        XjrBaseModule module = baseModuleService.getById(visualRelease.getModuleId());
                        if (module != null) {
                            result.put("type", 1);
                            result.put("value", module.getUrlAddress());
                        }
                    }
                }
            }
        } else {
            Optional<XjrBaseSubsystem> subsystem = Optional.ofNullable(subsystemService.getById(systemId));
            if (subsystem.isPresent()) {
                String indexUrl = subsystem.get().getIndexUrl();
                if (StringUtils.isNotEmpty(indexUrl)) {
                    String desktopschemeId = indexUrl.replace("/desktop-scheme/contentById/", "");
                    XjrBaseDesktopscheme xjrBaseDesktopscheme = desktopschemeService.getById(desktopschemeId);
                    if (xjrBaseDesktopscheme != null) {
                        Optional<XjrBaseDesktopschemeinfo> schemaInfo = Optional.ofNullable(desktopschemeinfoService.getOne(Wrappers.<XjrBaseDesktopschemeinfo>query().lambda().eq(XjrBaseDesktopschemeinfo::getSchemeId, xjrBaseDesktopscheme.getId()).eq(XjrBaseDesktopschemeinfo::getEnabledMark, EnabledMarkEnum.ENABLED.getCode())));
                        if (schemaInfo.isPresent()) {
                            result.put("type", 2);
                            result.put("value", schemaInfo.get().getContent());
                        }
                    }
                }
            }
        }
        return Response.ok(result);
    }
}
